//
//  ECAttributedStringBuildItem.swift
//  EasyCoding
//
//  Created by JY_NEW on 2021/8/19.
//

import Foundation

/// easy.build里面的每一个项目
public protocol ECAttributedStringBuildable {
    /// 构建字符串
    func __easyPerformBuildString() -> ECAttributedString?
}

// MARK:  空处理
public struct ECAttributedStringBuildItemNone: ECAttributedStringBuildable {
    public func __easyPerformBuildString() -> ECAttributedString? {
        return nil
    }
}

// MARK:  数组处理
public struct ECAttributedStringBuildItemArray: ECAttributedStringBuildable {
    var items: [ECAttributedStringBuildable]
    
    public func __easyPerformBuildString() -> ECAttributedString? {
        let string = ECAttributedString("")
        items.compactMap({ $0.__easyPerformBuildString() })
            .forEach(string.append(other:))
        return string
    }
}

// MARK:  单字符串处理

/// 默认可传String
extension String: ECAttributedStringBuildable {
    public func __easyPerformBuildString() -> ECAttributedString? {
        return ECAttributedString(self)
    }
}
/// 默认可传String
extension NSAttributedString: ECAttributedStringBuildable {
    public func __easyPerformBuildString() -> ECAttributedString? {
        let string = ECAttributedString("")
        string.append(other: self)
        return string
    }
}
/// 默认可传String
extension ECAttributedString: ECAttributedStringBuildable {
    public func __easyPerformBuildString() -> ECAttributedString? {
        return self
    }
}
